package reflect.constructor;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName("reflect.constructor.Student");
        Constructor<?> constructor = aClass.getConstructor(String.class, int.class);
        Object st = constructor.newInstance("于飞正", 23);
        System.out.println(st);
        //访问私有的构造方法
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
        //因为私有构造虽然可以访问，但是无法创建对象
        //因此需要暴力反射
        declaredConstructor.setAccessible(true);
        Object p = declaredConstructor.newInstance("杨昊");
        System.out.println(p);
    }
}
